<?php

namespace App\Http\Requests\Admin\PayAndConsume;

use App\Models\Enum\DataType;
use App\Services\PayAndConsume\HotspotService;
use Illuminate\Foundation\Http\FormRequest;

class HotspotIndexRequest extends FormRequest
{
    public function __construct(HotspotService $hotspotService)
    {
        $this->hotspotService = $hotspotService;
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'start_time' => 'before_or_equal:end_time',
            'end_time' => 'after_or_equal:start_date',
        ];
    }

    public function messages()
    {
        return [
            'start_time.before_or_equal' => '开始时间必须小于或等于结束时间',
            'end_time.after_or_equal' => '结束时间必须大于或等于开始时间',
        ];
    }

    protected function prepareForValidation()
    {
        $reqStartTime = isset($this->start_time) ? $this->start_time : date('Y-m-d');
        $reqEndTime = isset($this->end_time) ? $this->end_time : date('Y-m-d');

        //日期选择
        $reqDataType = isset($this->data_type) ? intval($this->data_type) : '0';
        if ($reqDataType > 0) {
            switch ($reqDataType) {
                case DataType::TODAY:
                    $reqStartTime = date('Y-m-d');
                    $reqEndTime = date('Y-m-d');
                    break;
                case DataType::YESTERDAY:
                    $reqStartTime = date('Y-m-d', strtotime('-1 day'));
                    $reqEndTime = date('Y-m-d', strtotime('-1 day'));
                    break;
                case DataType::SEVEN_DAYS:
                    $reqStartTime = date('Y-m-d', strtotime('-6 day'));
                    $reqEndTime = date('Y-m-d');
                    break;
                case DataType::THIRTY_DAYS:
                    $reqStartTime = date('Y-m-d', strtotime('-29 day'));
                    $reqEndTime = date('Y-m-d');
                    break;
                case DataType::HALF_YEAR:
                    $reqStartTime = date('Y-m-d', strtotime('-179 day'));
                    $reqEndTime = date('Y-m-d');
                    break;
            }
        }

        $this->merge([
            'start_time' => $reqStartTime,
            'end_time' => $reqEndTime,
            'data_type' => $reqDataType,
        ]);
    }
}
